home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 December / december_2000.iso / Intercd / root / Multimedia / audio / ^NoiseTracker / NtkSourceCode / cubicspline.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-03  |  1.8 KB  |  72 lines

  1. // CUBIC SPLINE FUNCTION
  2. //
  3. // Arguru
  4. //
  5.  
  6. class Cubic
  7. {
  8. public:
  9.     Cubic();
  10.  
  11.     float Work(float yo,float y0,float y1,float y2,unsigned __int32 res, long offset,long length);
  12.  
  13.     // Work function. Where all is cooked :]
  14.     // yo = y[-1] [sample at x-1]
  15.     // y0 = y[0]  [sample at x (input)]
  16.     // y1 = y[1]  [sample at x+1]
  17.     // y2 = y[2]  [sample at x+2]
  18.     
  19.     // res= distance between two neighboughs sample points [y0 and y1] 
  20.     //      ,so [0...1.0]. You have to multiply this distance * RESOLUTION used
  21.     //      on the  spline conversion table. [256 by default]
  22.     // If you are using 256 is asumed you are using 8 bit decimal
  23.     // fixed point offsets for resampling.
  24.  
  25.     // offset = sample offset [info to avoid go out of bounds on sample reading ]
  26.     // offset = sample length [info to avoid go out of bounds on sample reading ]
  27.  
  28. private:
  29.     int RESOLUTION; // Currently is 256, that's enough...
  30.     float at[1024];
  31.     float bt[1024];
  32.     float ct[1024];
  33.     float dt[1024];
  34.  
  35. };
  36.  
  37. Cubic::Cubic() /* The resampler constructor */
  38. {
  39. RESOLUTION=1024;
  40. // Initialize table...
  41. for (int i=0;i<RESOLUTION;i++)
  42. {
  43.     float x = (float)i/(float)RESOLUTION;
  44.     at[i] = -0.5*x*x*x+x*x-0.5*x;
  45.     bt[i] = 1.5*x*x*x-2.5*x*x+1;
  46.     ct[i] = -1.5*x*x*x+2*x*x+0.5*x;
  47.     dt[i] = 0.5*x*x*x-0.5*x*x;
  48. }
  49.  
  50. }
  51.  
  52. // Work body
  53.  
  54. float Cubic::Work(float yo,float y0,float y1,float y2,unsigned __int32 res, long offset,long length)
  55. {
  56.     res=res>>22;
  57.     if(offset==0)yo=0;
  58.     if(offset+2>length)y1=0;
  59.     if(offset+3>length)y2=0;
  60.  
  61.     return at[res]*yo+bt[res]*y0+ct[res]*y1+dt[res]*y2;
  62. }
  63.  
  64. // Despiste this CubicSpline function, only for debuggin purposes, not optimized
  65. float CubicSpline(float oy1,float y0,float y1, float y2, float x)
  66. {
  67. float a=(3*(y0-y1)-oy1+y2)*0.5;
  68. float b=2*y1+oy1-(5*y0+y2)*0.5;
  69. float c=(y1-oy1)*0.5;
  70. return a*x*x*x+b*x*x+c*x+y0;
  71. }
  72.